// pages/teacherInfo/teacherInfo.js
let request = require('../../operation/operation.js')
let bxt = require('../../utils/bxt.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    mode:'preview' // preview 提供编辑、解除关系入口；del 只提供解除关系入口
  },

  /**
   * 生命周期函数--监听页面加载
   * params options.mode=del(班级管理入口会传入)
   */
  onLoad: function (options) {
    console.log(options)
    let teacher = getApp().globalData.param, mode = 'preview'
    if (options.mode) {
      mode = options.mode
    }
    this.setData({
      mode:mode,
      teacher:teacher
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 点击编辑信息按钮事件处理函数
   */
  onEditTeacher:function() {
    wx.navigateTo({
      url: '../editTeacher/editTeacher?teacherSid=' + this.data.teacher.sid,
    })
  },

  onDelRelation:function() {
    wx.navigateTo({
      url: '../removeRelation/removeRelation?teacherSid=' + this.data.teacher.sid,
    })
  },

  onDeleteTeacher:function() {
    let that = this,teacher = this.data.teacher

    wx.showModal({
      title: '删除老师',
      content: '确定删除老师？',
      showCancel: true,
      success: function (res) {
        if (res.confirm) {
          request.deleteRequest('/teachers/' + teacher.sid, {}, true)
            .then(res => {
              var data = res.data;
              if (request.SUCCESSED == data.status) {
                getApp().notificationCenter.post(bxt.DELETE_TEACHER_MESSAGE, { teacher })

                // 更新用户信息
                getApp().notificationCenter.post(bxt.BIND_ROLE_MESSAGE)
                wx.showToast({
                  title: '删除成功',
                })
                setTimeout(function () {
                  wx.navigateBack({

                  })
                }
                  , 1500)

              } else {
                wx.showToast({
                  title: data.msg,
                  icon: 'none'
                })
              }
            })
            .catch(e => {
              console.log(e)
            })
        }
      }
    })
  }

})